var PageList=require('../../tools/update_goods/index.js')
var db=require('../../tools/database/index').merchants

var express=require('express')
var Router=express.Router()
//解析post data
var PostData=require('../../tools/PostData/index.js')
var dataLen=0;
// 定时更新数据库	 间隔时间	 time s
var time=60;
setInterval(async ()=>{
	var dataLen=await PageList.update()
	console.log('服务器数据流更新')
},1000*time)


//数据格式化
function factory(data){
	var list={
		name:data.title||'null',//商品名
		price:data.price||0,//价格
		classes:data.classes,//类别
		images:data.img,//展示图
		businessName:data.businessName||'',//businessName
		businessID:data.businessID||'',//商家Id
	}
}


//获取推荐商品信息
Router.post('/homepagedata',async (req,res)=>{
	var data=await PostData(req);
	var start=data.index||0;
	var List=await PageList.find(start*10,10)
	res.send(List)
})

//roduct_details
//获取商品详情
Router.get('/roduct_details',async (req,res)=>{
	const {businessID,index}=req.query;
	var Store=await db.queryInfo(businessID)
	console.log('商家信息:',Store)
	var result=await db.warehouse.getDate({
		'_id':businessID
	})
	
	res.send({
		...result.commodity[index],
		StoreName:Store.name,
		StoreHead:Store.HeadImg
	})
})

module.exports=Router